package com.haulmont.asu.web.ui.command_template;

import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.components.actions.EditAction;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

/**
 * Author: Sergey42
 * Date: 17.11.13 19:19
 */
public class CommandTemplateBrowser extends AbstractLookup {

    @Inject
    protected Table commandTable;

    @Override
    public void init(Map<String, Object> params) {
        replaceAddAction();
        replaceEditAction();
    }

    protected void replaceAddAction() {
        Action createAction = commandTable.getAction("create");
        commandTable.removeAction(createAction);

        commandTable.addAction(new CreateAction(commandTable, WindowManager.OpenType.NEW_TAB) {
            @Override
            public Map<String, Object> getWindowParams() {
                if (windowParams == null) {
                    windowParams = new HashMap<>();
                }

                windowParams.put("template", true);
                return windowParams;
            }

            @Override
            public String getWindowId() {
                return "ext$CommandTemplate.edit";
            }
        });
    }

    protected void replaceEditAction() {
        Action editAction = commandTable.getAction("edit");
        commandTable.removeAction(editAction);

        commandTable.addAction(new EditAction(commandTable, WindowManager.OpenType.NEW_TAB) {
            @Override
            public Map<String, Object> getWindowParams() {
                if (windowParams == null) {
                    windowParams = new HashMap<>();
                }

                windowParams.put("template", true);
                return windowParams;
            }

            @Override
            public String getWindowId() {
                return "ext$CommandTemplate.edit";
            }
        });
    }
}
